package com.aaa.tmr.controller;

import com.aaa.tmr.entity.Dictionary;
import com.aaa.tmr.entity.DictionaryType;
import com.aaa.tmr.service.DictionaryService;
import com.aaa.tmr.service.DictionaryTypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.aaa.tmr.util.Result;
import com.aaa.tmr.util.ReturnStatus;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Dictionary)表控制层
 *
 * @author makejava
 * @since 2021-03-21 22:44:58
 */
@Controller
@RequestMapping("dictionary")
public class DictionaryController {
    /**
     * 服务对象
     */
    @Resource
    private DictionaryService dictionaryService;

    @Resource
    private DictionaryTypeService dictionaryTypeService;

    /**
     * 跳转字典类型分页列表页面
     * @return 列表页面
     */
    @RequestMapping("toDictionary")
    public String toDictionary(){
        return "back/dictionary-page";
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Dictionary selectOne(Integer id) {
        return this.dictionaryService.queryById(id);
    }

    /**
     * 分页查询方法
     *
     * @param page
     * @param limit
     * @param dictionary
     * @return
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result<java.util.List> queryAll(Integer page, Integer limit, Dictionary dictionary) {
        PageInfo pageInfo = dictionaryService.queryAll(page, limit, dictionary);
        return new Result<>(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 查询全部字典类型
     *
     * @return ReturnBean
     * @createBy Enzo
     * @createTime 2020/12/29 22:47
     */
    @GetMapping("selectAllType")
    @ResponseBody
    public Result selectAllType() {
        List<DictionaryType> dictionaryTypes = dictionaryTypeService.queryAllType();
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),0L,dictionaryTypes);
    }
    /**
     * 新增数据
     *
     * @param dictionary 实例对象
     * @return 实例对象
     */
    @RequestMapping("insert")
    @ResponseBody
    public Result<Dictionary> insert(Dictionary dictionary) {
        return new Result<>(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0L,
                this.dictionaryService.insert(dictionary));
    }

    /**
     * 修改数据
     *
     * @param dictionary 实例对象
     * @return 实例对象
     */
    @PutMapping("update")
    @ResponseBody
    public Result<Dictionary> update(Dictionary dictionary) {
        return new Result<>(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0L,
                this.dictionaryService.update(dictionary));
    }

    /**
     * 通过主键删除数据
     *
     * @param dictionaryId 主键
     * @return 是否成功
     */
    
    @DeleteMapping("deleteById")
    @ResponseBody
    public Result<Boolean> deleteById(Integer dictionaryId) {
        return new Result<>(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0L,
                this.dictionaryService.deleteById(dictionaryId));
    }

    /**
     * description: 字典类型删除方法
     * @param dictionaryIds dictionaryIds
     * @return Result<Integer>
     * @author Marko
     * @Date 2021/3/21 18:14
     **/
    @DeleteMapping("deleteByIds")
    @ResponseBody
    public Result<Integer> deleteByIds(String dictionaryIds){
        return new Result<>(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage()
                ,0L,dictionaryService.deleteByIds(dictionaryIds));
    }

}
